<script setup>
import { ref } from 'vue'
import appStore from '@/store'
const { setToken, removeToken } = appStore.useUser
import MyApi from '@/service/api/my/my'
const labelPosition = 'top'
const formLabelAlign = ref({
  account: '',
  password: '',
  new_password: '',
  new_password1: ''
})
const rules = ref({
  account: [
    { required: true, message: '请输入账号', trigger: 'blur' },
    { min: 5, max: 10, message: '长度在 5 到 10 个字符', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, max: 12, message: '长度在 6 到 12 个字符', trigger: 'blur' }
  ],
  new_password: [
    { required: true, message: '请输入新密码', trigger: 'blur' },
    { min: 6, max: 12, message: '长度在 6 到 12 个字符', trigger: 'blur' }
  ],
  new_password1: [
    { required: true, message: '请再次输入新密码', trigger: 'blur' },
    { min: 6, max: 12, message: '长度在 6 到 12 个字符', trigger: 'blur' },
    {
      validator: () => {
        if (formLabelAlign.value.new_password !== formLabelAlign.value.new_password1) {
          return Promise.reject('两次输入的密码不一致')
        } else {
          return Promise.resolve()
        }
      },
      trigger: 'blur'
    }
  ]
})

const formEl = ref(null)

const handleChange = () => {
  ElMessageBox.confirm(`确认要修改密码吗`, '警告', {
    confirmButtonText: 'OK',
    cancelButtonText: 'Cancel',
    type: 'warning'
  })
    .then(async () => {
      await formEl.value.validate((valid, fields) => {
        if (valid) {
          console.log('submit!', formLabelAlign.value)
          MyApi.editMyInfoPassword(formLabelAlign.value).then((res) => {
            if (res.code == 200) {
              removeToken()
              ElMessage({
                type: 'success',
                message: '修改成功,请重新登录'
              })
              setTimeout(() => {
                window.location.href = '/login'
              }, 1000)
            }
          })
        } else {
          ElMessage({
            type: 'error',
            message: '请输入合法字段'
          })
        }
      })
    })
    .catch(() => {
      ElMessage({
        type: 'info',
        message: '修改失败'
      })
    })
}
</script>

<template>
  <div class="info-container">
    <el-form
      class="form"
      :label-position="labelPosition"
      label-width="80px"
      :model="formLabelAlign"
      :rules="rules"
      ref="formEl"
    >
      <el-form-item label="填写账号" prop="account">
        <el-input v-model="formLabelAlign.account"></el-input>
      </el-form-item>
      <el-form-item label="填写旧密码" prop="password">
        <el-input v-model="formLabelAlign.password"></el-input>
      </el-form-item>
      <el-form-item label="填写新密码" prop="new_password">
        <el-input v-model="formLabelAlign.new_password"></el-input>
      </el-form-item>
      <el-form-item label="再次填写新密码" prop="new_password1">
        <el-input v-model="formLabelAlign.new_password1"></el-input>
      </el-form-item>
    </el-form>
    <div class="btn">
      <el-button type="primary" class="confirm-btn" round plain @click="handleChange()"
        >修改密码</el-button
      >
    </div>
  </div>
</template>

<style scoped lang="scss">
.info-container {
  width: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  flex-direction: column;
  .btn {
    width: 70%;
  }
  .confirm-btn {
    margin: 5px 15px 10px 0px;
  }
  .form {
    margin-top: 20px;
    width: 70%;
  }
}
</style>
